<?php
/**
 * Created by PhpStorm.
 * User: chentaohua
 * Date: 2019/6/4
 * Time: 14:24
 */
use Illuminate\Support\Facades\Storage;

// 获取当前域名
function getDomain(){
    return $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'].'/';
}

/**
 * 从网上下载图片保存到服务器
 * @param $url 图片网址
 * @param $path 保存到服务器的路径
 */
function downloadImages($url,$path) {
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // 绕过ssl
    $img = curl_exec($ch);

    curl_close($ch);
    // $filename = pathinfo($url, PATHINFO_BASENAME); // 获取文件名
    //$image_name就是要保存到什么路径,默认只写文件名的话保存到根目录

    //保存的文件名称用的是链接里面的名称
    $fp = fopen($path,'w');
    fwrite($fp, $img);
    fclose($fp);
}


/**
 * 获取数字验证码
 * @param int $length 验证码长度
 */
function getNumCode($length = 6){

    $arr = [0,1,2,3,4,5,6,7,8,9];
    $str = '';
    for ($i = 0; $i < $length; $i++){
        $str .= $arr[rand(0,count($arr) - 1)];
    }
    return $str;
}


/**
 *获取唯一订单号
 * @return string
 */
function order_number(){
    return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}

/**
 * 图片上传
 */
function upload_img($file){

        if (!$file->isValid()) {
            return false;
        }
        // 文件扩展名
        $extension = $file->getClientOriginalExtension();
        // 文件名
        $fileName = $file->getClientOriginalName();
        // 生成新的统一格式的文件名
        $newFileName = md5($fileName . time() . mt_rand(1, 10000)) . '.' . $extension;
        // 图片保存路径
        $savePath = 'images/'.date('Ymd').'/'.time(). $newFileName;
       /* // Web 访问路径
        $webPath = '/storage/'. $savePath;*/

        // 将文件保存到本地 storage/app/public/images 目录下，先判断同名文件是否已经存在，如果存在直接返回
        if (Storage::disk('public')->has($savePath)) {

            return $savePath;
        }
        // 否则执行保存操作，保存成功将访问路径返回给调用方
        if (Storage::disk('upload')->put($savePath,file_get_contents($file->getRealPath()))) {

            return $savePath;
        }
        return false;
}

/**
 * 獲取隨機數
 * @return string0
 */
function getRandCode($len = 16){
    $arr = [
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'
    ];
    $tmpstr = '';

    for($i = 0;$i < $len;$i++){
        $tmpstr .= $arr[rand(0,count($arr) - 1)];
    }

    return $tmpstr;
}


function httpCurl($url = '',$type = 'get',$data = [],$header = []){

    // 初始化
    $ch = curl_init();

    // 设置curl参数
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // 绕过ssl
    curl_setopt($ch,CURLOPT_HTTPHEADER, $header); // 添加header信息

    // post提交需要提交参数
    if($type == 'post'){
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    }

    // 采集
    $result = curl_exec($ch);

    // 关闭
    curl_close($ch);
    return $result;
}


/**
 * sha256 加密
 */
function sha256($value){
    return hash('sha256',$value);
}

function random_bargain_money($max=100,$min=1){
    $num = $min + mt_rand() / mt_getrandmax() * ($max - $min);
    return sprintf("%.2f",$num);  //控制小数后几位
}
